package com.cy.核心编程.thead.basic.yellow.homework;

import java.util.List;
import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;

public class TableThread extends Thread {
    List<Ticket> ticketList;

    private static AtomicInteger i = new AtomicInteger();

    public TableThread(List<Ticket> ticketList, String name) {
        this.ticketList = ticketList;
        this.setName(name);
    }

    @Override
    public void run() {
        while (true){
            if(i.get()+4 < ticketList.size()) {
                try {
                    Thread.sleep(new Random().nextInt(150));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                Ticket ticket = ticketList.get(i.incrementAndGet());
                System.out.println(super.getName() + "卖出：" + ticket.getName());
            }else {
                System.out.println("票卖完了");
                break;
            }
        }
    }
}
